<?php

namespace Drawping\Amap\Webservice;

use Drawping\Amap\AbstractAPI;

class Webservice extends AbstractAPI
{
    const API_REGEO = 'http://restapi.amap.com/v3/geocode/regeo';
    const API_DISTANCE = 'http://restapi.amap.com/v3/distance';

    /**
     * 逆地理编码
     * @param $location
     * @param string $poitype
     * @param int $radius
     * @param string $extensions
     * @param bool $batch
     * @param int $roadlevel
     * @return bool|array
     */
    public function regeo($location, $poitype = '', $radius = 1000, $extensions = 'all', $batch = false, $roadlevel = 0)
    {
        $locations = implode('|', $location);

        $response = $this->http->request(
            'GET',
            self::API_REGEO,
            [
                'query' => [
                    'key' => $this->config['app_key'],
                    'location' => $locations,
                    'poitype' => $poitype,
                    'radius' => $radius,
                    'extensions' => $extensions,
                    'batch' => $batch ? 'true' : 'false',
                    'roadlevel' => $roadlevel
                ]
            ]
        );

        $r = $this->parseJSON($response);
        return $r ? ($batch ? $r->regeocodes : $r->regeocode) : false;
    }

    /**
     * 距离测量
     * @param string $origins 
     * @param string $destination
     * @param int $type
     * @return bool|array
     */
    function distance($origins = '', $destination = '', $type = 1)
    {
        $origins = implode('|', $origins);
        $response = $this->http->request(
            'GET',
            self::API_DISTANCE,
            [
                'query' => [
                    'key' => $this->config['app_key'],
                    'origins' => $origins,
                    'destination' => $destination,
                    'type' => $type
                ]
            ]
        );

        $r = $this->parseJSON($response);
        return $r ? $r->results : false;
    }
}
